home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / lib-tk / Tkinter.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  62.8 KB  |  1,966 lines

  1. # Tkinter.py -- Tk/Tcl widget wrappers
  2.  
  3. __version__ = "$Revision: 1.127 $"
  4.  
  5. import sys
  6. if sys.platform == "win32":
  7.     import FixTk # Attempt to configure Tcl/Tk without requiring PATH
  8. import _tkinter # If this fails your Python may not be configured for Tk
  9. tkinter = _tkinter # b/w compat for export
  10. TclError = _tkinter.TclError
  11. from types import *
  12. from Tkconstants import *
  13. import string; _string = string; del string
  14. try:
  15.     import MacOS; _MacOS = MacOS; del MacOS
  16. except ImportError:
  17.     _MacOS = None
  18.  
  19. TkVersion = _string.atof(_tkinter.TK_VERSION)
  20. TclVersion = _string.atof(_tkinter.TCL_VERSION)
  21.  
  22. READABLE = _tkinter.READABLE
  23. WRITABLE = _tkinter.WRITABLE
  24. EXCEPTION = _tkinter.EXCEPTION
  25.  
  26. # These are not always defined, e.g. not on Win32 with Tk 8.0 :-(
  27. try: _tkinter.createfilehandler
  28. except AttributeError: _tkinter.createfilehandler = None
  29. try: _tkinter.deletefilehandler
  30. except AttributeError: _tkinter.deletefilehandler = None
  31.     
  32.     
  33. def _flatten(tuple):
  34.     res = ()
  35.     for item in tuple:
  36.         if type(item) in (TupleType, ListType):
  37.             res = res + _flatten(item)
  38.         elif item is not None:
  39.             res = res + (item,)
  40.     return res
  41.  
  42. def _cnfmerge(cnfs):
  43.     if type(cnfs) is DictionaryType:
  44.         return cnfs
  45.     elif type(cnfs) in (NoneType, StringType):
  46.         return cnfs
  47.     else:
  48.         cnf = {}
  49.         for c in _flatten(cnfs):
  50.             try:
  51.                 cnf.update(c)
  52.             except (AttributeError, TypeError), msg:
  53.                 print "_cnfmerge: fallback due to:", msg
  54.                 for k, v in c.items():
  55.                     cnf[k] = v
  56.         return cnf
  57.  
  58. class Event:
  59.     pass
  60.  
  61. _support_default_root = 1
  62. _default_root = None
  63.  
  64. def NoDefaultRoot():
  65.     global _support_default_root
  66.     _support_default_root = 0
  67.     global _default_root
  68.     _default_root = None
  69.     del _default_root
  70.  
  71. def _tkerror(err):
  72.     pass
  73.  
  74. def _exit(code='0'):
  75.     raise SystemExit, code
  76.  
  77. _varnum = 0
  78. class Variable:
  79.     _default = ""
  80.     def __init__(self, master=None):
  81.         global _varnum
  82.         if not master:
  83.             master = _default_root
  84.         self._master = master
  85.         self._tk = master.tk
  86.         self._name = 'PY_VAR' + `_varnum`
  87.         _varnum = _varnum + 1
  88.         self.set(self._default)
  89.     def __del__(self):
  90.         self._tk.globalunsetvar(self._name)
  91.     def __str__(self):
  92.         return self._name
  93.     def set(self, value):
  94.         return self._tk.globalsetvar(self._name, value)
  95.     def trace_variable(self, mode, callback):
  96.         cbname = self._master._register(callback)
  97.         self._tk.call("trace", "variable", self._name, mode, cbname)
  98.         return cbname
  99.     trace = trace_variable
  100.     def trace_vdelete(self, mode, cbname):
  101.         self._tk.call("trace", "vdelete", self._name, mode, cbname)
  102.         self._master.deletecommand(cbname)
  103.     def trace_vinfo(self):
  104.         return map(self._tk.split, self._tk.splitlist(
  105.             self._tk.call("trace", "vinfo", self._name)))
  106.  
  107. class StringVar(Variable):
  108.     _default = ""
  109.     def __init__(self, master=None):
  110.         Variable.__init__(self, master)
  111.     def get(self):
  112.         return self._tk.globalgetvar(self._name)
  113.  
  114. class IntVar(Variable):
  115.     _default = 0
  116.     def __init__(self, master=None):
  117.         Variable.__init__(self, master)
  118.     def get(self):
  119.         return getint(self._tk.globalgetvar(self._name))
  120.  
  121. class DoubleVar(Variable):
  122.     _default = 0.0
  123.     def __init__(self, master=None):
  124.         Variable.__init__(self, master)
  125.     def get(self):
  126.         return getdouble(self._tk.globalgetvar(self._name))
  127.  
  128. class BooleanVar(Variable):
  129.     _default = "false"
  130.     def __init__(self, master=None):
  131.         Variable.__init__(self, master)
  132.     def get(self):
  133.         return self._tk.getboolean(self._tk.globalgetvar(self._name))
  134.  
  135. def mainloop(n=0):
  136.     _default_root.tk.mainloop(n)
  137.  
  138. getint = int
  139.  
  140. getdouble = float
  141.  
  142. def getboolean(s):
  143.     return _default_root.tk.getboolean(s)
  144.  
  145. # Methods defined on both toplevel and interior widgets
  146. class Misc:
  147.     # XXX font command?
  148.     _tclCommands = None
  149.     def destroy(self):
  150.         if self._tclCommands is not None:
  151.             for name in self._tclCommands:
  152.                 #print '- Tkinter: deleted command', name
  153.                 self.tk.deletecommand(name)
  154.             self._tclCommands = None
  155.     def deletecommand(self, name):
  156.         #print '- Tkinter: deleted command', name
  157.         self.tk.deletecommand(name)
  158.         try:
  159.             self._tclCommands.remove(name)
  160.         except ValueError:
  161.             pass
  162.     def tk_strictMotif(self, boolean=None):
  163.         return self.tk.getboolean(self.tk.call(
  164.             'set', 'tk_strictMotif', boolean))
  165.     def tk_bisque(self):
  166.         self.tk.call('tk_bisque')
  167.     def tk_setPalette(self, *args, **kw):
  168.         self.tk.call(('tk_setPalette',)
  169.               + _flatten(args) + _flatten(kw.items()))
  170.     def tk_menuBar(self, *args):
  171.         pass # obsolete since Tk 4.0
  172.     def wait_variable(self, name='PY_VAR'):
  173.         self.tk.call('tkwait', 'variable', name)
  174.     waitvar = wait_variable # XXX b/w compat
  175.     def wait_window(self, window=None):
  176.         if window == None:
  177.             window = self
  178.         self.tk.call('tkwait', 'window', window._w)
  179.     def wait_visibility(self, window=None):
  180.         if window == None:
  181.             window = self
  182.         self.tk.call('tkwait', 'visibility', window._w)
  183.     def setvar(self, name='PY_VAR', value='1'):
  184.         self.tk.setvar(name, value)
  185.     def getvar(self, name='PY_VAR'):
  186.         return self.tk.getvar(name)
  187.     getint = int
  188.     getdouble = float
  189.     def getboolean(self, s):
  190.         return self.tk.getboolean(s)
  191.     def focus_set(self):
  192.         self.tk.call('focus', self._w)
  193.     focus = focus_set # XXX b/w compat?
  194.     def focus_force(self):
  195.         self.tk.call('focus', '-force', self._w)
  196.     def focus_get(self):
  197.         name = self.tk.call('focus')
  198.         if name == 'none' or not name: return None
  199.         return self._nametowidget(name)
  200.     def focus_displayof(self):
  201.         name = self.tk.call('focus', '-displayof', self._w)
  202.         if name == 'none' or not name: return None
  203.         return self._nametowidget(name)
  204.     def focus_lastfor(self):
  205.         name = self.tk.call('focus', '-lastfor', self._w)
  206.         if name == 'none' or not name: return None
  207.         return self._nametowidget(name)
  208.     def tk_focusFollowsMouse(self):
  209.         self.tk.call('tk_focusFollowsMouse')
  210.     def tk_focusNext(self):
  211.         name = self.tk.call('tk_focusNext', self._w)
  212.         if not name: return None
  213.         return self._nametowidget(name)
  214.     def tk_focusPrev(self):
  215.         name = self.tk.call('tk_focusPrev', self._w)
  216.         if not name: return None
  217.         return self._nametowidget(name)
  218.     def after(self, ms, func=None, *args):
  219.         if not func:
  220.             # I'd rather use time.sleep(ms*0.001)
  221.             self.tk.call('after', ms)
  222.         else:
  223.             # XXX Disgusting hack to clean up after calling func
  224.             tmp = []
  225.             def callit(func=func, args=args, self=self, tmp=tmp):
  226.                 try:
  227.                     apply(func, args)
  228.                 finally:
  229.                     try:
  230.                         self.deletecommand(tmp[0])
  231.                     except TclError:
  232.                         pass
  233.             name = self._register(callit)
  234.             tmp.append(name)
  235.             return self.tk.call('after', ms, name)
  236.     def after_idle(self, func, *args):
  237.         return apply(self.after, ('idle', func) + args)
  238.     def after_cancel(self, id):
  239.         self.tk.call('after', 'cancel', id)
  240.     def bell(self, displayof=0):
  241.         self.tk.call(('bell',) + self._displayof(displayof))
  242.     # Clipboard handling:
  243.     def clipboard_clear(self, **kw):
  244.         if not kw.has_key('displayof'): kw['displayof'] = self._w
  245.         self.tk.call(('clipboard', 'clear') + self._options(kw))
  246.     def clipboard_append(self, string, **kw):
  247.         if not kw.has_key('displayof'): kw['displayof'] = self._w
  248.         self.tk.call(('clipboard', 'append') + self._options(kw)
  249.               + ('--', string))
  250.     # XXX grab current w/o window argument
  251.     def grab_current(self):
  252.         name = self.tk.call('grab', 'current', self._w)
  253.         if not name: return None
  254.         return self._nametowidget(name)
  255.     def grab_release(self):
  256.         self.tk.call('grab', 'release', self._w)
  257.     def grab_set(self):
  258.         self.tk.call('grab', 'set', self._w)
  259.     def grab_set_global(self):
  260.         self.tk.call('grab', 'set', '-global', self._w)
  261.     def grab_status(self):
  262.         status = self.tk.call('grab', 'status', self._w)
  263.         if status == 'none': status = None
  264.         return status
  265.     def lower(self, belowThis=None):
  266.         self.tk.call('lower', self._w, belowThis)
  267.     def option_add(self, pattern, value, priority = None):
  268.         self.tk.call('option', 'add', pattern, value, priority)
  269.     def option_clear(self):
  270.         self.tk.call('option', 'clear')
  271.     def option_get(self, name, className):
  272.         return self.tk.call('option', 'get', self._w, name, className)
  273.     def option_readfile(self, fileName, priority = None):
  274.         self.tk.call('option', 'readfile', fileName, priority)
  275.     def selection_clear(self, **kw):
  276.         if not kw.has_key('displayof'): kw['displayof'] = self._w
  277.         self.tk.call(('selection', 'clear') + self._options(kw))
  278.     def selection_get(self, **kw):
  279.         if not kw.has_key('displayof'): kw['displayof'] = self._w
  280.         return self.tk.call(('selection', 'get') + self._options(kw))
  281.     def selection_handle(self, command, **kw):
  282.         name = self._register(command)
  283.         self.tk.call(('selection', 'handle') + self._options(kw)
  284.               + (self._w, name))
  285.     def selection_own(self, **kw):
  286.         "Become owner of X selection."
  287.         self.tk.call(('selection', 'own') +
  288.                  self._options(kw) + (self._w,))
  289.     def selection_own_get(self, **kw):
  290.         "Find owner of X selection."
  291.         if not kw.has_key('displayof'): kw['displayof'] = self._w
  292.         name = self.tk.call(('selection', 'own') + self._options(kw))
  293.         if not name: return None
  294.         return self._nametowidget(name)
  295.     def send(self, interp, cmd, *args):
  296.         return self.tk.call(('send', interp, cmd) + args)
  297.     def lower(self, belowThis=None):
  298.         self.tk.call('lower', self._w, belowThis)
  299.     def tkraise(self, aboveThis=None):
  300.         self.tk.call('raise', self._w, aboveThis)
  301.     lift = tkraise
  302.     def colormodel(self, value=None):
  303.         return self.tk.call('tk', 'colormodel', self._w, value)
  304.     def winfo_atom(self, name, displayof=0):
  305.         args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
  306.         return getint(self.tk.call(args))
  307.     def winfo_atomname(self, id, displayof=0):
  308.         args = ('winfo', 'atomname') \
  309.                + self._displayof(displayof) + (id,)
  310.         return self.tk.call(args)
  311.     def winfo_cells(self):
  312.         return getint(
  313.             self.tk.call('winfo', 'cells', self._w))
  314.     def winfo_children(self):
  315.         return map(self._nametowidget,
  316.                self.tk.splitlist(self.tk.call(
  317.                    'winfo', 'children', self._w)))
  318.     def winfo_class(self):
  319.         return self.tk.call('winfo', 'class', self._w)
  320.     def winfo_colormapfull(self):
  321.         return self.tk.getboolean(
  322.             self.tk.call('winfo', 'colormapfull', self._w))
  323.     def winfo_containing(self, rootX, rootY, displayof=0):
  324.         args = ('winfo', 'containing') \
  325.                + self._displayof(displayof) + (rootX, rootY)
  326.         name = self.tk.call(args)
  327.         if not name: return None
  328.         return self._nametowidget(name)
  329.     def winfo_depth(self):
  330.         return getint(self.tk.call('winfo', 'depth', self._w))
  331.     def winfo_exists(self):
  332.         return getint(
  333.             self.tk.call('winfo', 'exists', self._w))
  334.     def winfo_fpixels(self, number):
  335.         return getdouble(self.tk.call(
  336.             'winfo', 'fpixels', self._w, number))
  337.     def winfo_geometry(self):
  338.         return self.tk.call('winfo', 'geometry', self._w)
  339.     def winfo_height(self):
  340.         return getint(
  341.             self.tk.call('winfo', 'height', self._w))
  342.     def winfo_id(self):
  343.         return self.tk.getint(
  344.             self.tk.call('winfo', 'id', self._w))
  345.     def winfo_interps(self, displayof=0):
  346.         args = ('winfo', 'interps') + self._displayof(displayof)
  347.         return self.tk.splitlist(self.tk.call(args))
  348.     def winfo_ismapped(self):
  349.         return getint(
  350.             self.tk.call('winfo', 'ismapped', self._w))
  351.     def winfo_manager(self):
  352.         return self.tk.call('winfo', 'manager', self._w)
  353.     def winfo_name(self):
  354.         return self.tk.call('winfo', 'name', self._w)
  355.     def winfo_parent(self):
  356.         return self.tk.call('winfo', 'parent', self._w)
  357.     def winfo_pathname(self, id, displayof=0):
  358.         args = ('winfo', 'pathname') \
  359.                + self._displayof(displayof) + (id,)
  360.         return self.tk.call(args)
  361.     def winfo_pixels(self, number):
  362.         return getint(
  363.             self.tk.call('winfo', 'pixels', self._w, number))
  364.     def winfo_pointerx(self):
  365.         return getint(
  366.             self.tk.call('winfo', 'pointerx', self._w))
  367.     def winfo_pointerxy(self):
  368.         return self._getints(
  369.             self.tk.call('winfo', 'pointerxy', self._w))
  370.     def winfo_pointery(self):
  371.         return getint(
  372.             self.tk.call('winfo', 'pointery', self._w))
  373.     def winfo_reqheight(self):
  374.         return getint(
  375.             self.tk.call('winfo', 'reqheight', self._w))
  376.     def winfo_reqwidth(self):
  377.         return getint(
  378.             self.tk.call('winfo', 'reqwidth', self._w))
  379.     def winfo_rgb(self, color):
  380.         return self._getints(
  381.             self.tk.call('winfo', 'rgb', self._w, color))
  382.     def winfo_rootx(self):
  383.         return getint(
  384.             self.tk.call('winfo', 'rootx', self._w))
  385.     def winfo_rooty(self):
  386.         return getint(
  387.             self.tk.call('winfo', 'rooty', self._w))
  388.     def winfo_screen(self):
  389.         return self.tk.call('winfo', 'screen', self._w)
  390.     def winfo_screencells(self):
  391.         return getint(
  392.             self.tk.call('winfo', 'screencells', self._w))
  393.     def winfo_screendepth(self):
  394.         return getint(
  395.             self.tk.call('winfo', 'screendepth', self._w))
  396.     def winfo_screenheight(self):
  397.         return getint(
  398.             self.tk.call('winfo', 'screenheight', self._w))
  399.     def winfo_screenmmheight(self):
  400.         return getint(
  401.             self.tk.call('winfo', 'screenmmheight', self._w))
  402.     def winfo_screenmmwidth(self):
  403.         return getint(
  404.             self.tk.call('winfo', 'screenmmwidth', self._w))
  405.     def winfo_screenvisual(self):
  406.         return self.tk.call('winfo', 'screenvisual', self._w)
  407.     def winfo_screenwidth(self):
  408.         return getint(
  409.             self.tk.call('winfo', 'screenwidth', self._w))
  410.     def winfo_server(self):
  411.         return self.tk.call('winfo', 'server', self._w)
  412.     def winfo_toplevel(self):
  413.         return self._nametowidget(self.tk.call(
  414.             'winfo', 'toplevel', self._w))
  415.     def winfo_viewable(self):
  416.         return getint(
  417.             self.tk.call('winfo', 'viewable', self._w))
  418.     def winfo_visual(self):
  419.         return self.tk.call('winfo', 'visual', self._w)
  420.     def winfo_visualid(self):
  421.         return self.tk.call('winfo', 'visualid', self._w)
  422.     def winfo_visualsavailable(self, includeids=0):
  423.         data = self.tk.split(
  424.             self.tk.call('winfo', 'visualsavailable', self._w,
  425.                      includeids and 'includeids' or None))
  426.         def parseitem(x, self=self):
  427.             return x[:1] + tuple(map(getint, x[1:]))
  428.         return map(parseitem, data)
  429.     def winfo_vrootheight(self):
  430.         return getint(
  431.             self.tk.call('winfo', 'vrootheight', self._w))
  432.     def winfo_vrootwidth(self):
  433.         return getint(
  434.             self.tk.call('winfo', 'vrootwidth', self._w))
  435.     def winfo_vrootx(self):
  436.         return getint(
  437.             self.tk.call('winfo', 'vrootx', self._w))
  438.     def winfo_vrooty(self):
  439.         return getint(
  440.             self.tk.call('winfo', 'vrooty', self._w))
  441.     def winfo_width(self):
  442.         return getint(
  443.             self.tk.call('winfo', 'width', self._w))
  444.     def winfo_x(self):
  445.         return getint(
  446.             self.tk.call('winfo', 'x', self._w))
  447.     def winfo_y(self):
  448.         return getint(
  449.             self.tk.call('winfo', 'y', self._w))
  450.     def update(self):
  451.         self.tk.call('update')
  452.     def update_idletasks(self):
  453.         self.tk.call('update', 'idletasks')
  454.     def bindtags(self, tagList=None):
  455.         if tagList is None:
  456.             return self.tk.splitlist(
  457.                 self.tk.call('bindtags', self._w))
  458.         else:
  459.             self.tk.call('bindtags', self._w, tagList)
  460.     def _bind(self, what, sequence, func, add, needcleanup=1):
  461.         if type(func) is StringType:
  462.             self.tk.call(what + (sequence, func))
  463.         elif func:
  464.             funcid = self._register(func, self._substitute,
  465.                         needcleanup)
  466.             cmd = ('%sif {"[%s %s]" == "break"} break\n'
  467.                    %
  468.                    (add and '+' or '',
  469.                 funcid,
  470.                 _string.join(self._subst_format)))
  471.             self.tk.call(what + (sequence, cmd))
  472.             return funcid
  473.         elif sequence:
  474.             return self.tk.call(what + (sequence,))
  475.         else:
  476.             return self.tk.splitlist(self.tk.call(what))
  477.     def bind(self, sequence=None, func=None, add=None):
  478.         return self._bind(('bind', self._w), sequence, func, add)
  479.     def unbind(self, sequence, funcid=None):
  480.         self.tk.call('bind', self._w, sequence, '')
  481.         if funcid:
  482.             self.deletecommand(funcid)
  483.     def bind_all(self, sequence=None, func=None, add=None):
  484.         return self._bind(('bind', 'all'), sequence, func, add, 0)
  485.     def unbind_all(self, sequence):
  486.         self.tk.call('bind', 'all' , sequence, '')
  487.     def bind_class(self, className, sequence=None, func=None, add=None):
  488.         return self._bind(('bind', className), sequence, func, add, 0)
  489.     def unbind_class(self, className, sequence):
  490.         self.tk.call('bind', className , sequence, '')
  491.     def mainloop(self, n=0):
  492.         self.tk.mainloop(n)
  493.     def quit(self):
  494.         self.tk.quit()
  495.     def _getints(self, string):
  496.         if string:
  497.             return tuple(map(getint, self.tk.splitlist(string)))
  498.     def _getdoubles(self, string):
  499.         if string:
  500.             return tuple(map(getdouble, self.tk.splitlist(string)))
  501.     def _getboolean(self, string):
  502.         if string:
  503.             return self.tk.getboolean(string)
  504.     def _displayof(self, displayof):
  505.         if displayof:
  506.             return ('-displayof', displayof)
  507.         if displayof is None:
  508.             return ('-displayof', self._w)
  509.         return ()
  510.     def _options(self, cnf, kw = None):
  511.         if kw:
  512.             cnf = _cnfmerge((cnf, kw))
  513.         else:
  514.             cnf = _cnfmerge(cnf)
  515.         res = ()
  516.         for k, v in cnf.items():
  517.             if v is not None:
  518.                 if k[-1] == '_': k = k[:-1]
  519.                 if callable(v):
  520.                     v = self._register(v)
  521.                 res = res + ('-'+k, v)
  522.         return res
  523.     def nametowidget(self, name):
  524.         w = self
  525.         if name[0] == '.':
  526.             w = w._root()
  527.             name = name[1:]
  528.         find = _string.find
  529.         while name:
  530.             i = find(name, '.')
  531.             if i >= 0:
  532.                 name, tail = name[:i], name[i+1:]
  533.             else:
  534.                 tail = ''
  535.             w = w.children[name]
  536.             name = tail
  537.         return w
  538.     _nametowidget = nametowidget
  539.     def _register(self, func, subst=None, needcleanup=1):
  540.         f = CallWrapper(func, subst, self).__call__
  541.         name = `id(f)`
  542.         try:
  543.             func = func.im_func
  544.         except AttributeError:
  545.             pass
  546.         try:
  547.             name = name + func.__name__
  548.         except AttributeError:
  549.             pass
  550.         self.tk.createcommand(name, f)
  551.         if needcleanup:
  552.             if self._tclCommands is None:
  553.                 self._tclCommands = []
  554.             self._tclCommands.append(name)
  555.         #print '+ Tkinter created command', name
  556.         return name
  557.     register = _register
  558.     def _root(self):
  559.         w = self
  560.         while w.master: w = w.master
  561.         return w
  562.     _subst_format = ('%#', '%b', '%f', '%h', '%k', 
  563.              '%s', '%t', '%w', '%x', '%y',
  564.              '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
  565.     def _substitute(self, *args):
  566.         if len(args) != len(self._subst_format): return args
  567.         getboolean = self.tk.getboolean
  568.         getint = int
  569.         nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
  570.         # Missing: (a, c, d, m, o, v, B, R)
  571.         e = Event()
  572.         e.serial = getint(nsign)
  573.         e.num = getint(b)
  574.         try: e.focus = getboolean(f)
  575.         except TclError: pass
  576.         e.height = getint(h)
  577.         e.keycode = getint(k)
  578.         # For Visibility events, event state is a string and
  579.         # not an integer:
  580.         try:
  581.             e.state = getint(s)
  582.         except ValueError:
  583.             e.state = s
  584.         e.time = getint(t)
  585.         e.width = getint(w)
  586.         e.x = getint(x)
  587.         e.y = getint(y)
  588.         e.char = A
  589.         try: e.send_event = getboolean(E)
  590.         except TclError: pass
  591.         e.keysym = K
  592.         e.keysym_num = getint(N)
  593.         e.type = T
  594.         try:
  595.             e.widget = self._nametowidget(W)
  596.         except KeyError:
  597.             e.widget = W
  598.         e.x_root = getint(X)
  599.         e.y_root = getint(Y)
  600.         return (e,)
  601.     def _report_exception(self):
  602.         import sys
  603.         exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
  604.         root = self._root()
  605.         root.report_callback_exception(exc, val, tb)
  606.     # These used to be defined in Widget:
  607.     def configure(self, cnf=None, **kw):
  608.         # XXX ought to generalize this so tag_config etc. can use it
  609.         if kw:
  610.             cnf = _cnfmerge((cnf, kw))
  611.         elif cnf:
  612.             cnf = _cnfmerge(cnf)
  613.         if cnf is None:
  614.             cnf = {}
  615.             for x in self.tk.split(
  616.                 self.tk.call(self._w, 'configure')):
  617.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  618.             return cnf
  619.         if type(cnf) is StringType:
  620.             x = self.tk.split(self.tk.call(
  621.                 self._w, 'configure', '-'+cnf))
  622.             return (x[0][1:],) + x[1:]
  623.         self.tk.call((self._w, 'configure')
  624.               + self._options(cnf))
  625.     config = configure
  626.     def cget(self, key):
  627.         return self.tk.call(self._w, 'cget', '-' + key)
  628.     __getitem__ = cget
  629.     def __setitem__(self, key, value):
  630.         self.configure({key: value})
  631.     def keys(self):
  632.         return map(lambda x: x[0][1:],
  633.                self.tk.split(self.tk.call(self._w, 'configure')))
  634.     def __str__(self):
  635.         return self._w
  636.     # Pack methods that apply to the master
  637.     _noarg_ = ['_noarg_']
  638.     def pack_propagate(self, flag=_noarg_):
  639.         if flag is Misc._noarg_:
  640.             return self._getboolean(self.tk.call(
  641.                 'pack', 'propagate', self._w))
  642.         else:
  643.             self.tk.call('pack', 'propagate', self._w, flag)
  644.     propagate = pack_propagate
  645.     def pack_slaves(self):
  646.         return map(self._nametowidget,
  647.                self.tk.splitlist(
  648.                    self.tk.call('pack', 'slaves', self._w)))
  649.     slaves = pack_slaves
  650.     # Place method that applies to the master
  651.     def place_slaves(self):
  652.         return map(self._nametowidget,
  653.                self.tk.splitlist(
  654.                    self.tk.call(
  655.                        'place', 'slaves', self._w)))
  656.     # Grid methods that apply to the master
  657.     def grid_bbox(self, column=None, row=None, col2=None, row2=None):
  658.         args = ('grid', 'bbox', self._w)
  659.         if column is not None and row is not None:
  660.             args = args + (column, row)
  661.         if col2 is not None and row2 is not None:
  662.             args = args + (col2, row2)
  663.         return self._getints(apply(self.tk.call, args)) or None
  664.  
  665.     bbox = grid_bbox
  666.     def _grid_configure(self, command, index, cnf, kw):
  667.         if type(cnf) is StringType and not kw:
  668.             if cnf[-1:] == '_':
  669.                 cnf = cnf[:-1]
  670.             if cnf[:1] != '-':
  671.                 cnf = '-'+cnf
  672.             options = (cnf,)
  673.         else:
  674.             options = self._options(cnf, kw)
  675.         if not options:
  676.             res = self.tk.call('grid',
  677.                        command, self._w, index)
  678.             words = self.tk.splitlist(res)
  679.             dict = {}
  680.             for i in range(0, len(words), 2):
  681.                 key = words[i][1:]
  682.                 value = words[i+1]
  683.                 if not value:
  684.                     value = None
  685.                 elif '.' in value:
  686.                     value = getdouble(value)
  687.                 else:
  688.                     value = getint(value)
  689.                 dict[key] = value
  690.             return dict
  691.         res = self.tk.call(
  692.                   ('grid', command, self._w, index) 
  693.                   + options)
  694.         if len(options) == 1:
  695.             if not res: return None
  696.             # In Tk 7.5, -width can be a float
  697.             if '.' in res: return getdouble(res)
  698.             return getint(res)
  699.     def grid_columnconfigure(self, index, cnf={}, **kw):
  700.         return self._grid_configure('columnconfigure', index, cnf, kw)
  701.     columnconfigure = grid_columnconfigure
  702.     def grid_propagate(self, flag=_noarg_):
  703.         if flag is Misc._noarg_:
  704.             return self._getboolean(self.tk.call(
  705.                 'grid', 'propagate', self._w))
  706.         else:
  707.             self.tk.call('grid', 'propagate', self._w, flag)
  708.     def grid_rowconfigure(self, index, cnf={}, **kw):
  709.         return self._grid_configure('rowconfigure', index, cnf, kw)
  710.     rowconfigure = grid_rowconfigure
  711.     def grid_size(self):
  712.         return self._getints(
  713.             self.tk.call('grid', 'size', self._w)) or None
  714.     size = grid_size
  715.     def grid_slaves(self, row=None, column=None):
  716.         args = ()
  717.         if row is not None:
  718.             args = args + ('-row', row)
  719.         if column is not None:
  720.             args = args + ('-column', column)
  721.         return map(self._nametowidget,
  722.                self.tk.splitlist(self.tk.call(
  723.                    ('grid', 'slaves', self._w) + args)))
  724.  
  725.     # Support for the "event" command, new in Tk 4.2.
  726.     # By Case Roole.
  727.  
  728.     def event_add(self, virtual, *sequences):
  729.         args = ('event', 'add', virtual) + sequences
  730.         self.tk.call(args)
  731.  
  732.     def event_delete(self, virtual, *sequences):
  733.         args = ('event', 'delete', virtual) + sequences
  734.         self.tk.call(args)
  735.  
  736.     def event_generate(self, sequence, **kw):
  737.         args = ('event', 'generate', self._w, sequence)
  738.         for k, v in kw.items():
  739.             args = args + ('-%s' % k, str(v))
  740.         self.tk.call(args)
  741.  
  742.     def event_info(self, virtual=None):
  743.         return self.tk.splitlist(
  744.             self.tk.call('event', 'info', virtual))
  745.  
  746.     # Image related commands
  747.  
  748.     def image_names(self):
  749.         return self.tk.call('image', 'names')
  750.  
  751.     def image_types(self):
  752.         return self.tk.call('image', 'types')
  753.  
  754.  
  755. class CallWrapper:
  756.     def __init__(self, func, subst, widget):
  757.         self.func = func
  758.         self.subst = subst
  759.         self.widget = widget
  760.     def __call__(self, *args):
  761.         try:
  762.             if self.subst:
  763.                 args = apply(self.subst, args)
  764.             return apply(self.func, args)
  765.         except SystemExit, msg:
  766.             raise SystemExit, msg
  767.         except:
  768.             self.widget._report_exception()
  769.  
  770.  
  771. class Wm:
  772.     def wm_aspect(self, 
  773.            minNumer=None, minDenom=None, 
  774.            maxNumer=None, maxDenom=None):
  775.         return self._getints(
  776.             self.tk.call('wm', 'aspect', self._w, 
  777.                      minNumer, minDenom, 
  778.                      maxNumer, maxDenom))
  779.     aspect = wm_aspect
  780.     def wm_client(self, name=None):
  781.         return self.tk.call('wm', 'client', self._w, name)
  782.     client = wm_client
  783.     def wm_colormapwindows(self, *wlist):
  784.         args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
  785.         return map(self._nametowidget, self.tk.call(args))
  786.     colormapwindows = wm_colormapwindows
  787.     def wm_command(self, value=None):
  788.         return self.tk.call('wm', 'command', self._w, value)
  789.     command = wm_command
  790.     def wm_deiconify(self):
  791.         return self.tk.call('wm', 'deiconify', self._w)
  792.     deiconify = wm_deiconify
  793.     def wm_focusmodel(self, model=None):
  794.         return self.tk.call('wm', 'focusmodel', self._w, model)
  795.     focusmodel = wm_focusmodel
  796.     def wm_frame(self):
  797.         return self.tk.call('wm', 'frame', self._w)
  798.     frame = wm_frame
  799.     def wm_geometry(self, newGeometry=None):
  800.         return self.tk.call('wm', 'geometry', self._w, newGeometry)
  801.     geometry = wm_geometry
  802.     def wm_grid(self,
  803.          baseWidth=None, baseHeight=None, 
  804.          widthInc=None, heightInc=None):
  805.         return self._getints(self.tk.call(
  806.             'wm', 'grid', self._w,
  807.             baseWidth, baseHeight, widthInc, heightInc))
  808.     grid = wm_grid
  809.     def wm_group(self, pathName=None):
  810.         return self.tk.call('wm', 'group', self._w, pathName)
  811.     group = wm_group
  812.     def wm_iconbitmap(self, bitmap=None):
  813.         return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
  814.     iconbitmap = wm_iconbitmap
  815.     def wm_iconify(self):
  816.         return self.tk.call('wm', 'iconify', self._w)
  817.     iconify = wm_iconify
  818.     def wm_iconmask(self, bitmap=None):
  819.         return self.tk.call('wm', 'iconmask', self._w, bitmap)
  820.     iconmask = wm_iconmask
  821.     def wm_iconname(self, newName=None):
  822.         return self.tk.call('wm', 'iconname', self._w, newName)
  823.     iconname = wm_iconname
  824.     def wm_iconposition(self, x=None, y=None):
  825.         return self._getints(self.tk.call(
  826.             'wm', 'iconposition', self._w, x, y))
  827.     iconposition = wm_iconposition
  828.     def wm_iconwindow(self, pathName=None):
  829.         return self.tk.call('wm', 'iconwindow', self._w, pathName)
  830.     iconwindow = wm_iconwindow
  831.     def wm_maxsize(self, width=None, height=None):
  832.         return self._getints(self.tk.call(
  833.             'wm', 'maxsize', self._w, width, height))
  834.     maxsize = wm_maxsize
  835.     def wm_minsize(self, width=None, height=None):
  836.         return self._getints(self.tk.call(
  837.             'wm', 'minsize', self._w, width, height))
  838.     minsize = wm_minsize
  839.     def wm_overrideredirect(self, boolean=None):
  840.         return self._getboolean(self.tk.call(
  841.             'wm', 'overrideredirect', self._w, boolean))
  842.     overrideredirect = wm_overrideredirect
  843.     def wm_positionfrom(self, who=None):
  844.         return self.tk.call('wm', 'positionfrom', self._w, who)
  845.     positionfrom = wm_positionfrom
  846.     def wm_protocol(self, name=None, func=None):
  847.         if callable(func):
  848.             command = self._register(func)
  849.         else:
  850.             command = func
  851.         return self.tk.call(
  852.             'wm', 'protocol', self._w, name, command)
  853.     protocol = wm_protocol
  854.     def wm_resizable(self, width=None, height=None):
  855.         return self.tk.call('wm', 'resizable', self._w, width, height)
  856.     resizable = wm_resizable
  857.     def wm_sizefrom(self, who=None):
  858.         return self.tk.call('wm', 'sizefrom', self._w, who)
  859.     sizefrom = wm_sizefrom
  860.     def wm_state(self):
  861.         return self.tk.call('wm', 'state', self._w)
  862.     state = wm_state
  863.     def wm_title(self, string=None):
  864.         return self.tk.call('wm', 'title', self._w, string)
  865.     title = wm_title
  866.     def wm_transient(self, master=None):
  867.         return self.tk.call('wm', 'transient', self._w, master)
  868.     transient = wm_transient
  869.     def wm_withdraw(self):
  870.         return self.tk.call('wm', 'withdraw', self._w)
  871.     withdraw = wm_withdraw
  872.  
  873.  
  874. class Tk(Misc, Wm):
  875.     _w = '.'
  876.     def __init__(self, screenName=None, baseName=None, className='Tk'):
  877.         global _default_root
  878.         self.master = None
  879.         self.children = {}
  880.         if baseName is None:
  881.             import sys, os
  882.             baseName = os.path.basename(sys.argv[0])
  883.             baseName, ext = os.path.splitext(baseName)
  884.             if ext not in ('.py', '.pyc', '.pyo'):
  885.                 baseName = baseName + ext
  886.         self.tk = _tkinter.create(screenName, baseName, className)
  887.         if _MacOS:
  888.             # Disable event scanning except for Command-Period
  889.             _MacOS.SchedParams(1, 0)
  890.             # Work around nasty MacTk bug
  891.             # XXX Is this one still needed?
  892.             self.update()
  893.         # Version sanity checks
  894.         tk_version = self.tk.getvar('tk_version')
  895.         if tk_version != _tkinter.TK_VERSION:
  896.             raise RuntimeError, \
  897.             "tk.h version (%s) doesn't match libtk.a version (%s)" \
  898.             % (_tkinter.TK_VERSION, tk_version)
  899.         tcl_version = self.tk.getvar('tcl_version')
  900.         if tcl_version != _tkinter.TCL_VERSION:
  901.             raise RuntimeError, \
  902.             "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
  903.             % (_tkinter.TCL_VERSION, tcl_version)
  904.         if TkVersion < 4.0:
  905.             raise RuntimeError, \
  906.             "Tk 4.0 or higher is required; found Tk %s" \
  907.             % str(TkVersion)
  908.         self.tk.createcommand('tkerror', _tkerror)
  909.         self.tk.createcommand('exit', _exit)
  910.         self.readprofile(baseName, className)
  911.         if _support_default_root and not _default_root:
  912.             _default_root = self
  913.     def destroy(self):
  914.         for c in self.children.values(): c.destroy()
  915.         self.tk.call('destroy', self._w)
  916.         Misc.destroy(self)
  917.         global _default_root
  918.         if _support_default_root and _default_root is self:
  919.             _default_root = None
  920.     def readprofile(self, baseName, className):
  921.         import os
  922.         if os.environ.has_key('HOME'): home = os.environ['HOME']
  923.         else: home = os.curdir
  924.         class_tcl = os.path.join(home, '.%s.tcl' % className)
  925.         class_py = os.path.join(home, '.%s.py' % className)
  926.         base_tcl = os.path.join(home, '.%s.tcl' % baseName)
  927.         base_py = os.path.join(home, '.%s.py' % baseName)
  928.         dir = {'self': self}
  929.         exec 'from Tkinter import *' in dir
  930.         if os.path.isfile(class_tcl):
  931.             print 'source', `class_tcl`
  932.             self.tk.call('source', class_tcl)
  933.         if os.path.isfile(class_py):
  934.             print 'execfile', `class_py`
  935.             execfile(class_py, dir)
  936.         if os.path.isfile(base_tcl):
  937.             print 'source', `base_tcl`
  938.             self.tk.call('source', base_tcl)
  939.         if os.path.isfile(base_py):
  940.             print 'execfile', `base_py`
  941.             execfile(base_py, dir)
  942.     def report_callback_exception(self, exc, val, tb):
  943.         import traceback, sys
  944.         sys.stderr.write("Exception in Tkinter callback\n")
  945.         sys.last_type = exc
  946.         sys.last_value = val
  947.         sys.last_traceback = tb
  948.         traceback.print_exception(exc, val, tb)
  949.  
  950. # Ideally, the classes Pack, Place and Grid disappear, the
  951. # pack/place/grid methods are defined on the Widget class, and
  952. # everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
  953. # ...), with pack(), place() and grid() being short for
  954. # pack_configure(), place_configure() and grid_columnconfigure(), and
  955. # forget() being short for pack_forget().  As a practical matter, I'm
  956. # afraid that there is too much code out there that may be using the
  957. # Pack, Place or Grid class, so I leave them intact -- but only as
  958. # backwards compatibility features.  Also note that those methods that
  959. # take a master as argument (e.g. pack_propagate) have been moved to
  960. # the Misc class (which now incorporates all methods common between
  961. # toplevel and interior widgets).  Again, for compatibility, these are
  962. # copied into the Pack, Place or Grid class.
  963.  
  964. class Pack:
  965.     def pack_configure(self, cnf={}, **kw):
  966.         self.tk.call(
  967.               ('pack', 'configure', self._w) 
  968.               + self._options(cnf, kw))
  969.     pack = configure = config = pack_configure
  970.     def pack_forget(self):
  971.         self.tk.call('pack', 'forget', self._w)
  972.     forget = pack_forget
  973.     def pack_info(self):
  974.         words = self.tk.splitlist(
  975.             self.tk.call('pack', 'info', self._w))
  976.         dict = {}
  977.         for i in range(0, len(words), 2):
  978.             key = words[i][1:]
  979.             value = words[i+1]
  980.             if value[:1] == '.':
  981.                 value = self._nametowidget(value)
  982.             dict[key] = value
  983.         return dict
  984.     info = pack_info
  985.     propagate = pack_propagate = Misc.pack_propagate
  986.     slaves = pack_slaves = Misc.pack_slaves
  987.  
  988. class Place:
  989.     def place_configure(self, cnf={}, **kw):
  990.         for k in ['in_']:
  991.             if kw.has_key(k):
  992.                 kw[k[:-1]] = kw[k]
  993.                 del kw[k]
  994.         self.tk.call(
  995.               ('place', 'configure', self._w) 
  996.               + self._options(cnf, kw))
  997.     place = configure = config = place_configure
  998.     def place_forget(self):
  999.         self.tk.call('place', 'forget', self._w)
  1000.     forget = place_forget
  1001.     def place_info(self):
  1002.         words = self.tk.splitlist(
  1003.             self.tk.call('place', 'info', self._w))
  1004.         dict = {}
  1005.         for i in range(0, len(words), 2):
  1006.             key = words[i][1:]
  1007.             value = words[i+1]
  1008.             if value[:1] == '.':
  1009.                 value = self._nametowidget(value)
  1010.             dict[key] = value
  1011.         return dict
  1012.     info = place_info
  1013.     slaves = place_slaves = Misc.place_slaves
  1014.  
  1015. class Grid:
  1016.     # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
  1017.     def grid_configure(self, cnf={}, **kw):
  1018.         self.tk.call(
  1019.               ('grid', 'configure', self._w) 
  1020.               + self._options(cnf, kw))
  1021.     grid = configure = config = grid_configure
  1022.     bbox = grid_bbox = Misc.grid_bbox
  1023.     columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
  1024.     def grid_forget(self):
  1025.         self.tk.call('grid', 'forget', self._w)
  1026.     forget = grid_forget
  1027.     def grid_info(self):
  1028.         words = self.tk.splitlist(
  1029.             self.tk.call('grid', 'info', self._w))
  1030.         dict = {}
  1031.         for i in range(0, len(words), 2):
  1032.             key = words[i][1:]
  1033.             value = words[i+1]
  1034.             if value[:1] == '.':
  1035.                 value = self._nametowidget(value)
  1036.             dict[key] = value
  1037.         return dict
  1038.     info = grid_info
  1039.     def grid_location(self, x, y):
  1040.         return self._getints(
  1041.             self.tk.call(
  1042.                 'grid', 'location', self._w, x, y)) or None
  1043.     location = grid_location
  1044.     propagate = grid_propagate = Misc.grid_propagate
  1045.     rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
  1046.     size = grid_size = Misc.grid_size
  1047.     slaves = grid_slaves = Misc.grid_slaves
  1048.  
  1049. class BaseWidget(Misc):
  1050.     def _setup(self, master, cnf):
  1051.         if _support_default_root:
  1052.             global _default_root
  1053.             if not master:
  1054.                 if not _default_root:
  1055.                     _default_root = Tk()
  1056.                 master = _default_root
  1057.         self.master = master
  1058.         self.tk = master.tk
  1059.         name = None
  1060.         if cnf.has_key('name'):
  1061.             name = cnf['name']
  1062.             del cnf['name']
  1063.         if not name:
  1064.             name = `id(self)`
  1065.         self._name = name
  1066.         if master._w=='.':
  1067.             self._w = '.' + name
  1068.         else:
  1069.             self._w = master._w + '.' + name
  1070.         self.children = {}
  1071.         if self.master.children.has_key(self._name):
  1072.             self.master.children[self._name].destroy()
  1073.         self.master.children[self._name] = self
  1074.     def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
  1075.         if kw:
  1076.             cnf = _cnfmerge((cnf, kw))
  1077.         self.widgetName = widgetName
  1078.         BaseWidget._setup(self, master, cnf)
  1079.         classes = []
  1080.         for k in cnf.keys():
  1081.             if type(k) is ClassType:
  1082.                 classes.append((k, cnf[k]))
  1083.                 del cnf[k]
  1084.         self.tk.call(
  1085.             (widgetName, self._w) + extra + self._options(cnf))
  1086.         for k, v in classes:
  1087.             k.configure(self, v)
  1088.     def destroy(self):
  1089.         for c in self.children.values(): c.destroy()
  1090.         if self.master.children.has_key(self._name):
  1091.             del self.master.children[self._name]
  1092.         self.tk.call('destroy', self._w)
  1093.         Misc.destroy(self)
  1094.     def _do(self, name, args=()):
  1095.         # XXX Obsolete -- better use self.tk.call directly!
  1096.         return self.tk.call((self._w, name) + args)
  1097.  
  1098. class Widget(BaseWidget, Pack, Place, Grid):
  1099.     pass
  1100.  
  1101. class Toplevel(BaseWidget, Wm):
  1102.     def __init__(self, master=None, cnf={}, **kw):
  1103.         if kw:
  1104.             cnf = _cnfmerge((cnf, kw))
  1105.         extra = ()
  1106.         for wmkey in ['screen', 'class_', 'class', 'visual',
  1107.                   'colormap']:
  1108.             if cnf.has_key(wmkey):
  1109.                 val = cnf[wmkey]
  1110.                 # TBD: a hack needed because some keys
  1111.                 # are not valid as keyword arguments
  1112.                 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
  1113.                 else: opt = '-'+wmkey
  1114.                 extra = extra + (opt, val)
  1115.                 del cnf[wmkey]
  1116.         BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
  1117.         root = self._root()
  1118.         self.iconname(root.iconname())
  1119.         self.title(root.title())
  1120.  
  1121. class Button(Widget):
  1122.     def __init__(self, master=None, cnf={}, **kw):
  1123.         Widget.__init__(self, master, 'button', cnf, kw)
  1124.     def tkButtonEnter(self, *dummy):
  1125.         self.tk.call('tkButtonEnter', self._w)
  1126.     def tkButtonLeave(self, *dummy):
  1127.         self.tk.call('tkButtonLeave', self._w)
  1128.     def tkButtonDown(self, *dummy):
  1129.         self.tk.call('tkButtonDown', self._w)
  1130.     def tkButtonUp(self, *dummy):
  1131.         self.tk.call('tkButtonUp', self._w)
  1132.     def tkButtonInvoke(self, *dummy):
  1133.         self.tk.call('tkButtonInvoke', self._w)
  1134.     def flash(self):
  1135.         self.tk.call(self._w, 'flash')
  1136.     def invoke(self):
  1137.         return self.tk.call(self._w, 'invoke')
  1138.  
  1139. # Indices:
  1140. # XXX I don't like these -- take them away
  1141. def AtEnd():
  1142.     return 'end'
  1143. def AtInsert(*args):
  1144.     s = 'insert'
  1145.     for a in args:
  1146.         if a: s = s + (' ' + a)
  1147.     return s
  1148. def AtSelFirst():
  1149.     return 'sel.first'
  1150. def AtSelLast():
  1151.     return 'sel.last'
  1152. def At(x, y=None):
  1153.     if y is None:
  1154.         return '@' + `x`        
  1155.     else:
  1156.         return '@' + `x` + ',' + `y`
  1157.  
  1158. class Canvas(Widget):
  1159.     _tagcommands = None
  1160.     def __init__(self, master=None, cnf={}, **kw):
  1161.         Widget.__init__(self, master, 'canvas', cnf, kw)
  1162.     def addtag(self, *args):
  1163.         self.tk.call((self._w, 'addtag') + args)
  1164.     def addtag_above(self, newtag, tagOrId):
  1165.         self.addtag(newtag, 'above', tagOrId)
  1166.     def addtag_all(self, newtag):
  1167.         self.addtag(newtag, 'all')
  1168.     def addtag_below(self, newtag, tagOrId):
  1169.         self.addtag(newtag, 'below', tagOrId)
  1170.     def addtag_closest(self, newtag, x, y, halo=None, start=None):
  1171.         self.addtag(newtag, 'closest', x, y, halo, start)
  1172.     def addtag_enclosed(self, newtag, x1, y1, x2, y2):
  1173.         self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
  1174.     def addtag_overlapping(self, newtag, x1, y1, x2, y2):
  1175.         self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
  1176.     def addtag_withtag(self, newtag, tagOrId):
  1177.         self.addtag(newtag, 'withtag', tagOrId)
  1178.     def bbox(self, *args):
  1179.         return self._getints(
  1180.             self.tk.call((self._w, 'bbox') + args)) or None
  1181.     def tag_unbind(self, tagOrId, sequence, funcid=None):
  1182.         self.tk.call(self._w, 'bind', tagOrId, sequence, '')
  1183.         if funcid:
  1184.             self.deletecommand(funcid)
  1185.     def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
  1186.         res = self._bind((self._w, 'bind', tagOrId),
  1187.                  sequence, func, add)
  1188.         if sequence and func and res:
  1189.             # remember the funcid for later
  1190.             if self._tagcommands is None:
  1191.                 self._tagcommands = {}
  1192.             list = self._tagcommands.get(tagOrId) or []
  1193.             self._tagcommands[tagOrId] = list
  1194.             list.append(res)
  1195.         return res
  1196.     def canvasx(self, screenx, gridspacing=None):
  1197.         return getdouble(self.tk.call(
  1198.             self._w, 'canvasx', screenx, gridspacing))
  1199.     def canvasy(self, screeny, gridspacing=None):
  1200.         return getdouble(self.tk.call(
  1201.             self._w, 'canvasy', screeny, gridspacing))
  1202.     def coords(self, *args):
  1203.         # XXX Should use _flatten on args
  1204.         return map(getdouble,
  1205.                            self.tk.splitlist(
  1206.                    self.tk.call((self._w, 'coords') + args)))
  1207.     def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
  1208.         args = _flatten(args)
  1209.         cnf = args[-1]
  1210.         if type(cnf) in (DictionaryType, TupleType):
  1211.             args = args[:-1]
  1212.         else:
  1213.             cnf = {}
  1214.         return getint(apply(
  1215.             self.tk.call,
  1216.             (self._w, 'create', itemType) 
  1217.             + args + self._options(cnf, kw)))
  1218.     def create_arc(self, *args, **kw):
  1219.         return self._create('arc', args, kw)
  1220.     def create_bitmap(self, *args, **kw):
  1221.         return self._create('bitmap', args, kw)
  1222.     def create_image(self, *args, **kw):
  1223.         return self._create('image', args, kw)
  1224.     def create_line(self, *args, **kw):
  1225.         return self._create('line', args, kw)
  1226.     def create_oval(self, *args, **kw):
  1227.         return self._create('oval', args, kw)
  1228.     def create_polygon(self, *args, **kw):
  1229.         return self._create('polygon', args, kw)
  1230.     def create_rectangle(self, *args, **kw):
  1231.         return self._create('rectangle', args, kw)
  1232.     def create_text(self, *args, **kw):
  1233.         return self._create('text', args, kw)
  1234.     def create_window(self, *args, **kw):
  1235.         return self._create('window', args, kw)
  1236.     def dchars(self, *args):
  1237.         self.tk.call((self._w, 'dchars') + args)
  1238.     def delete(self, *args):
  1239.         self.tk.call((self._w, 'delete') + args)
  1240.     def dtag(self, *args):
  1241.         self.tk.call((self._w, 'dtag') + args)
  1242.     def find(self, *args):
  1243.         return self._getints(
  1244.             self.tk.call((self._w, 'find') + args)) or ()
  1245.     def find_above(self, tagOrId):
  1246.         return self.find('above', tagOrId)
  1247.     def find_all(self):
  1248.         return self.find('all')
  1249.     def find_below(self, tagOrId):
  1250.         return self.find('below', tagOrId)
  1251.     def find_closest(self, x, y, halo=None, start=None):
  1252.         return self.find('closest', x, y, halo, start)
  1253.     def find_enclosed(self, x1, y1, x2, y2):
  1254.         return self.find('enclosed', x1, y1, x2, y2)
  1255.     def find_overlapping(self, x1, y1, x2, y2):
  1256.         return self.find('overlapping', x1, y1, x2, y2)
  1257.     def find_withtag(self, tagOrId):
  1258.         return self.find('withtag', tagOrId)
  1259.     def focus(self, *args):
  1260.         return self.tk.call((self._w, 'focus') + args)
  1261.     def gettags(self, *args):
  1262.         return self.tk.splitlist(
  1263.             self.tk.call((self._w, 'gettags') + args))
  1264.     def icursor(self, *args):
  1265.         self.tk.call((self._w, 'icursor') + args)
  1266.     def index(self, *args):
  1267.         return getint(self.tk.call((self._w, 'index') + args))
  1268.     def insert(self, *args):
  1269.         self.tk.call((self._w, 'insert') + args)
  1270.     def itemcget(self, tagOrId, option):
  1271.         return self.tk.call(
  1272.             (self._w, 'itemcget') + (tagOrId, '-'+option))
  1273.     def itemconfigure(self, tagOrId, cnf=None, **kw):
  1274.         if cnf is None and not kw:
  1275.             cnf = {}
  1276.             for x in self.tk.split(
  1277.                 self.tk.call(self._w,
  1278.                          'itemconfigure', tagOrId)):
  1279.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  1280.             return cnf
  1281.         if type(cnf) == StringType and not kw:
  1282.             x = self.tk.split(self.tk.call(
  1283.                 self._w, 'itemconfigure', tagOrId, '-'+cnf))
  1284.             return (x[0][1:],) + x[1:]
  1285.         self.tk.call((self._w, 'itemconfigure', tagOrId) +
  1286.                  self._options(cnf, kw))
  1287.     itemconfig = itemconfigure
  1288.     # lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift,
  1289.     # so the preferred name for them is tag_lower, tag_raise
  1290.     # (similar to tag_bind, and similar to the Text widget);
  1291.     # unfortunately can't delete the old ones yet (maybe in 1.6)
  1292.     def tag_lower(self, *args):
  1293.         self.tk.call((self._w, 'lower') + args)
  1294.     lower = tag_lower
  1295.     def move(self, *args):
  1296.         self.tk.call((self._w, 'move') + args)
  1297.     def postscript(self, cnf={}, **kw):
  1298.         return self.tk.call((self._w, 'postscript') +
  1299.                     self._options(cnf, kw))
  1300.     def tag_raise(self, *args):
  1301.         self.tk.call((self._w, 'raise') + args)
  1302.     lift = tkraise = tag_raise
  1303.     def scale(self, *args):
  1304.         self.tk.call((self._w, 'scale') + args)
  1305.     def scan_mark(self, x, y):
  1306.         self.tk.call(self._w, 'scan', 'mark', x, y)
  1307.     def scan_dragto(self, x, y):
  1308.         self.tk.call(self._w, 'scan', 'dragto', x, y)
  1309.     def select_adjust(self, tagOrId, index):
  1310.         self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
  1311.     def select_clear(self):
  1312.         self.tk.call(self._w, 'select', 'clear')
  1313.     def select_from(self, tagOrId, index):
  1314.         self.tk.call(self._w, 'select', 'from', tagOrId, index)
  1315.     def select_item(self):
  1316.         self.tk.call(self._w, 'select', 'item')
  1317.     def select_to(self, tagOrId, index):
  1318.         self.tk.call(self._w, 'select', 'to', tagOrId, index)
  1319.     def type(self, tagOrId):
  1320.         return self.tk.call(self._w, 'type', tagOrId) or None
  1321.     def xview(self, *args):
  1322.         if not args:
  1323.             return self._getdoubles(self.tk.call(self._w, 'xview'))
  1324.         self.tk.call((self._w, 'xview') + args)
  1325.     def xview_moveto(self, fraction):
  1326.         self.tk.call(self._w, 'xview', 'moveto', fraction)
  1327.     def xview_scroll(self, number, what):
  1328.         self.tk.call(self._w, 'xview', 'scroll', number, what)
  1329.     def yview(self, *args):
  1330.         if not args:
  1331.             return self._getdoubles(self.tk.call(self._w, 'yview'))
  1332.         self.tk.call((self._w, 'yview') + args)
  1333.     def yview_moveto(self, fraction):
  1334.         self.tk.call(self._w, 'yview', 'moveto', fraction)
  1335.     def yview_scroll(self, number, what):
  1336.         self.tk.call(self._w, 'yview', 'scroll', number, what)
  1337.  
  1338. class Checkbutton(Widget):
  1339.     def __init__(self, master=None, cnf={}, **kw):
  1340.         Widget.__init__(self, master, 'checkbutton', cnf, kw)
  1341.     def deselect(self):
  1342.         self.tk.call(self._w, 'deselect')
  1343.     def flash(self):
  1344.         self.tk.call(self._w, 'flash')
  1345.     def invoke(self):
  1346.         return self.tk.call(self._w, 'invoke')
  1347.     def select(self):
  1348.         self.tk.call(self._w, 'select')
  1349.     def toggle(self):
  1350.         self.tk.call(self._w, 'toggle')
  1351.  
  1352. class Entry(Widget):
  1353.     def __init__(self, master=None, cnf={}, **kw):
  1354.         Widget.__init__(self, master, 'entry', cnf, kw)
  1355.     def delete(self, first, last=None):
  1356.         self.tk.call(self._w, 'delete', first, last)
  1357.     def get(self):
  1358.         return self.tk.call(self._w, 'get')
  1359.     def icursor(self, index):
  1360.         self.tk.call(self._w, 'icursor', index)
  1361.     def index(self, index):
  1362.         return getint(self.tk.call(
  1363.             self._w, 'index', index))
  1364.     def insert(self, index, string):
  1365.         self.tk.call(self._w, 'insert', index, string)
  1366.     def scan_mark(self, x):
  1367.         self.tk.call(self._w, 'scan', 'mark', x)
  1368.     def scan_dragto(self, x):
  1369.         self.tk.call(self._w, 'scan', 'dragto', x)
  1370.     def selection_adjust(self, index):
  1371.         self.tk.call(self._w, 'selection', 'adjust', index)
  1372.     select_adjust = selection_adjust
  1373.     def selection_clear(self):
  1374.         self.tk.call(self._w, 'selection', 'clear')
  1375.     select_clear = selection_clear
  1376.     def selection_from(self, index):
  1377.         self.tk.call(self._w, 'selection', 'from', index)
  1378.     select_from = selection_from
  1379.     def selection_present(self):
  1380.         return self.tk.getboolean(
  1381.             self.tk.call(self._w, 'selection', 'present'))
  1382.     select_present = selection_present
  1383.     def selection_range(self, start, end):
  1384.         self.tk.call(self._w, 'selection', 'range', start, end)
  1385.     select_range = selection_range
  1386.     def selection_to(self, index):
  1387.         self.tk.call(self._w, 'selection', 'to', index)
  1388.     select_to = selection_to
  1389.     def xview(self, index):
  1390.         self.tk.call(self._w, 'xview', index)
  1391.     def xview_moveto(self, fraction):
  1392.         self.tk.call(self._w, 'xview', 'moveto', fraction)
  1393.     def xview_scroll(self, number, what):
  1394.         self.tk.call(self._w, 'xview', 'scroll', number, what)
  1395.  
  1396. class Frame(Widget):
  1397.     def __init__(self, master=None, cnf={}, **kw):
  1398.         cnf = _cnfmerge((cnf, kw))
  1399.         extra = ()
  1400.         if cnf.has_key('class_'):
  1401.             extra = ('-class', cnf['class_'])
  1402.             del cnf['class_']
  1403.         elif cnf.has_key('class'):
  1404.             extra = ('-class', cnf['class'])
  1405.             del cnf['class']
  1406.         Widget.__init__(self, master, 'frame', cnf, {}, extra)
  1407.  
  1408. class Label(Widget):
  1409.     def __init__(self, master=None, cnf={}, **kw):
  1410.         Widget.__init__(self, master, 'label', cnf, kw)
  1411.  
  1412. class Listbox(Widget):
  1413.     def __init__(self, master=None, cnf={}, **kw):
  1414.         Widget.__init__(self, master, 'listbox', cnf, kw)
  1415.     def activate(self, index):
  1416.         self.tk.call(self._w, 'activate', index)
  1417.     def bbox(self, *args):
  1418.         return self._getints(
  1419.             self.tk.call((self._w, 'bbox') + args)) or None
  1420.     def curselection(self):
  1421.         # XXX Ought to apply self._getints()...
  1422.         return self.tk.splitlist(self.tk.call(
  1423.             self._w, 'curselection'))
  1424.     def delete(self, first, last=None):
  1425.         self.tk.call(self._w, 'delete', first, last)
  1426.     def get(self, first, last=None):
  1427.         if last:
  1428.             return self.tk.splitlist(self.tk.call(
  1429.                 self._w, 'get', first, last))
  1430.         else:
  1431.             return self.tk.call(self._w, 'get', first)
  1432.     def index(self, index):
  1433.         i = self.tk.call(self._w, 'index', index)
  1434.         if i == 'none': return None
  1435.         return getint(i)
  1436.     def insert(self, index, *elements):
  1437.         self.tk.call((self._w, 'insert', index) + elements)
  1438.     def nearest(self, y):
  1439.         return getint(self.tk.call(
  1440.             self._w, 'nearest', y))
  1441.     def scan_mark(self, x, y):
  1442.         self.tk.call(self._w, 'scan', 'mark', x, y)
  1443.     def scan_dragto(self, x, y):
  1444.         self.tk.call(self._w, 'scan', 'dragto', x, y)
  1445.     def see(self, index):
  1446.         self.tk.call(self._w, 'see', index)
  1447.     def selection_anchor(self, index):
  1448.         self.tk.call(self._w, 'selection', 'anchor', index)
  1449.     select_anchor = selection_anchor
  1450.     def selection_clear(self, first, last=None):
  1451.         self.tk.call(self._w,
  1452.                  'selection', 'clear', first, last)
  1453.     select_clear = selection_clear
  1454.     def selection_includes(self, index):
  1455.         return self.tk.getboolean(self.tk.call(
  1456.             self._w, 'selection', 'includes', index))
  1457.     select_includes = selection_includes
  1458.     def selection_set(self, first, last=None):
  1459.         self.tk.call(self._w, 'selection', 'set', first, last)
  1460.     select_set = selection_set
  1461.     def size(self):
  1462.         return getint(self.tk.call(self._w, 'size'))
  1463.     def xview(self, *what):
  1464.         if not what:
  1465.             return self._getdoubles(self.tk.call(self._w, 'xview'))
  1466.         self.tk.call((self._w, 'xview') + what)
  1467.     def xview_moveto(self, fraction):
  1468.         self.tk.call(self._w, 'xview', 'moveto', fraction)
  1469.     def xview_scroll(self, number, what):
  1470.         self.tk.call(self._w, 'xview', 'scroll', number, what)
  1471.     def yview(self, *what):
  1472.         if not what:
  1473.             return self._getdoubles(self.tk.call(self._w, 'yview'))
  1474.         self.tk.call((self._w, 'yview') + what)
  1475.     def yview_moveto(self, fraction):
  1476.         self.tk.call(self._w, 'yview', 'moveto', fraction)
  1477.     def yview_scroll(self, number, what):
  1478.         self.tk.call(self._w, 'yview', 'scroll', number, what)
  1479.  
  1480. class Menu(Widget):
  1481.     def __init__(self, master=None, cnf={}, **kw):
  1482.         Widget.__init__(self, master, 'menu', cnf, kw)
  1483.     def tk_bindForTraversal(self):
  1484.         pass # obsolete since Tk 4.0
  1485.     def tk_mbPost(self):
  1486.         self.tk.call('tk_mbPost', self._w)
  1487.     def tk_mbUnpost(self):
  1488.         self.tk.call('tk_mbUnpost')
  1489.     def tk_traverseToMenu(self, char):
  1490.         self.tk.call('tk_traverseToMenu', self._w, char)
  1491.     def tk_traverseWithinMenu(self, char):
  1492.         self.tk.call('tk_traverseWithinMenu', self._w, char)
  1493.     def tk_getMenuButtons(self):
  1494.         return self.tk.call('tk_getMenuButtons', self._w)
  1495.     def tk_nextMenu(self, count):
  1496.         self.tk.call('tk_nextMenu', count)
  1497.     def tk_nextMenuEntry(self, count):
  1498.         self.tk.call('tk_nextMenuEntry', count)
  1499.     def tk_invokeMenu(self):
  1500.         self.tk.call('tk_invokeMenu', self._w)
  1501.     def tk_firstMenu(self):
  1502.         self.tk.call('tk_firstMenu', self._w)
  1503.     def tk_mbButtonDown(self):
  1504.         self.tk.call('tk_mbButtonDown', self._w)
  1505.     def tk_popup(self, x, y, entry=""):
  1506.         self.tk.call('tk_popup', self._w, x, y, entry)
  1507.     def activate(self, index):
  1508.         self.tk.call(self._w, 'activate', index)
  1509.     def add(self, itemType, cnf={}, **kw):
  1510.         self.tk.call((self._w, 'add', itemType) +
  1511.                  self._options(cnf, kw))
  1512.     def add_cascade(self, cnf={}, **kw):
  1513.         self.add('cascade', cnf or kw)
  1514.     def add_checkbutton(self, cnf={}, **kw):
  1515.         self.add('checkbutton', cnf or kw)
  1516.     def add_command(self, cnf={}, **kw):
  1517.         self.add('command', cnf or kw)
  1518.     def add_radiobutton(self, cnf={}, **kw):
  1519.         self.add('radiobutton', cnf or kw)
  1520.     def add_separator(self, cnf={}, **kw):
  1521.         self.add('separator', cnf or kw)
  1522.     def insert(self, index, itemType, cnf={}, **kw):
  1523.         self.tk.call((self._w, 'insert', index, itemType) +
  1524.                  self._options(cnf, kw))
  1525.     def insert_cascade(self, index, cnf={}, **kw):
  1526.         self.insert(index, 'cascade', cnf or kw)
  1527.     def insert_checkbutton(self, index, cnf={}, **kw):
  1528.         self.insert(index, 'checkbutton', cnf or kw)
  1529.     def insert_command(self, index, cnf={}, **kw):
  1530.         self.insert(index, 'command', cnf or kw)
  1531.     def insert_radiobutton(self, index, cnf={}, **kw):
  1532.         self.insert(index, 'radiobutton', cnf or kw)
  1533.     def insert_separator(self, index, cnf={}, **kw):
  1534.         self.insert(index, 'separator', cnf or kw)
  1535.     def delete(self, index1, index2=None):
  1536.         self.tk.call(self._w, 'delete', index1, index2)
  1537.     def entrycget(self, index, option):
  1538.         return self.tk.call(self._w, 'entrycget', index, '-' + option)
  1539.     def entryconfigure(self, index, cnf=None, **kw):
  1540.         if cnf is None and not kw:
  1541.             cnf = {}
  1542.             for x in self.tk.split(self.tk.call(
  1543.                 (self._w, 'entryconfigure', index))):
  1544.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  1545.             return cnf
  1546.         if type(cnf) == StringType and not kw:
  1547.             x = self.tk.split(self.tk.call(
  1548.                 (self._w, 'entryconfigure', index, '-'+cnf)))
  1549.             return (x[0][1:],) + x[1:]
  1550.         self.tk.call((self._w, 'entryconfigure', index)
  1551.               + self._options(cnf, kw))
  1552.     entryconfig = entryconfigure
  1553.     def index(self, index):
  1554.         i = self.tk.call(self._w, 'index', index)
  1555.         if i == 'none': return None
  1556.         return getint(i)
  1557.     def invoke(self, index):
  1558.         return self.tk.call(self._w, 'invoke', index)
  1559.     def post(self, x, y):
  1560.         self.tk.call(self._w, 'post', x, y)
  1561.     def type(self, index):
  1562.         return self.tk.call(self._w, 'type', index)
  1563.     def unpost(self):
  1564.         self.tk.call(self._w, 'unpost')
  1565.     def yposition(self, index):
  1566.         return getint(self.tk.call(
  1567.             self._w, 'yposition', index))
  1568.  
  1569. class Menubutton(Widget):
  1570.     def __init__(self, master=None, cnf={}, **kw):
  1571.         Widget.__init__(self, master, 'menubutton', cnf, kw)
  1572.  
  1573. class Message(Widget):
  1574.     def __init__(self, master=None, cnf={}, **kw):
  1575.         Widget.__init__(self, master, 'message', cnf, kw)
  1576.  
  1577. class Radiobutton(Widget):
  1578.     def __init__(self, master=None, cnf={}, **kw):
  1579.         Widget.__init__(self, master, 'radiobutton', cnf, kw)
  1580.     def deselect(self):
  1581.         self.tk.call(self._w, 'deselect')
  1582.     def flash(self):
  1583.         self.tk.call(self._w, 'flash')
  1584.     def invoke(self):
  1585.         return self.tk.call(self._w, 'invoke')
  1586.     def select(self):
  1587.         self.tk.call(self._w, 'select')
  1588.  
  1589. class Scale(Widget):
  1590.     def __init__(self, master=None, cnf={}, **kw):
  1591.         Widget.__init__(self, master, 'scale', cnf, kw)
  1592.     def get(self):
  1593.         value = self.tk.call(self._w, 'get')
  1594.         try:
  1595.             return getint(value)
  1596.         except ValueError:
  1597.             return getdouble(value)
  1598.     def set(self, value):
  1599.         self.tk.call(self._w, 'set', value)
  1600.     def coords(self, value=None):
  1601.         return self._getints(self.tk.call(self._w, 'coords', value))
  1602.     def identify(self, x, y):
  1603.         return self.tk.call(self._w, 'identify', x, y)
  1604.  
  1605. class Scrollbar(Widget):
  1606.     def __init__(self, master=None, cnf={}, **kw):
  1607.         Widget.__init__(self, master, 'scrollbar', cnf, kw)
  1608.     def activate(self, index):
  1609.         self.tk.call(self._w, 'activate', index)
  1610.     def delta(self, deltax, deltay):
  1611.         return getdouble(
  1612.             self.tk.call(self._w, 'delta', deltax, deltay))
  1613.     def fraction(self, x, y):
  1614.         return getdouble(self.tk.call(self._w, 'fraction', x, y))
  1615.     def identify(self, x, y):
  1616.         return self.tk.call(self._w, 'identify', x, y)
  1617.     def get(self):
  1618.         return self._getdoubles(self.tk.call(self._w, 'get'))
  1619.     def set(self, *args):
  1620.         self.tk.call((self._w, 'set') + args)
  1621.  
  1622. class Text(Widget):
  1623.     # XXX Add dump()
  1624.     def __init__(self, master=None, cnf={}, **kw):
  1625.         Widget.__init__(self, master, 'text', cnf, kw)
  1626.     def bbox(self, *args):
  1627.         return self._getints(
  1628.             self.tk.call((self._w, 'bbox') + args)) or None
  1629.     def tk_textSelectTo(self, index):
  1630.         self.tk.call('tk_textSelectTo', self._w, index)
  1631.     def tk_textBackspace(self):
  1632.         self.tk.call('tk_textBackspace', self._w)
  1633.     def tk_textIndexCloser(self, a, b, c):
  1634.         self.tk.call('tk_textIndexCloser', self._w, a, b, c)
  1635.     def tk_textResetAnchor(self, index):
  1636.         self.tk.call('tk_textResetAnchor', self._w, index)
  1637.     def compare(self, index1, op, index2):
  1638.         return self.tk.getboolean(self.tk.call(
  1639.             self._w, 'compare', index1, op, index2))
  1640.     def debug(self, boolean=None):
  1641.         return self.tk.getboolean(self.tk.call(
  1642.             self._w, 'debug', boolean))
  1643.     def delete(self, index1, index2=None):
  1644.         self.tk.call(self._w, 'delete', index1, index2)
  1645.     def dlineinfo(self, index):
  1646.         return self._getints(self.tk.call(self._w, 'dlineinfo', index))
  1647.     def get(self, index1, index2=None):
  1648.         return self.tk.call(self._w, 'get', index1, index2)
  1649.     # (Image commands are new in 8.0)
  1650.     def image_cget(self, index, option):
  1651.         if option[:1] != "-":
  1652.             option = "-" + option
  1653.         if option[-1:] == "_":
  1654.             option = option[:-1]
  1655.         return self.tk.call(self._w, "image", "cget", index, option)
  1656.     def image_configure(self, index, cnf={}, **kw):
  1657.         if not cnf and not kw:
  1658.             cnf = {}
  1659.             for x in self.tk.split(
  1660.                     self.tk.call(
  1661.                     self._w, "image", "configure", index)):
  1662.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  1663.             return cnf
  1664.         apply(self.tk.call,
  1665.               (self._w, "image", "configure", index)
  1666.               + self._options(cnf, kw))
  1667.     def image_create(self, index, cnf={}, **kw):
  1668.         return apply(self.tk.call,
  1669.                  (self._w, "image", "create", index)
  1670.                  + self._options(cnf, kw))
  1671.     def image_names(self):
  1672.         return self.tk.call(self._w, "image", "names")
  1673.     def index(self, index):
  1674.         return self.tk.call(self._w, 'index', index)
  1675.     def insert(self, index, chars, *args):
  1676.         self.tk.call((self._w, 'insert', index, chars) + args)
  1677.     def mark_gravity(self, markName, direction=None):
  1678.         return self.tk.call(
  1679.             (self._w, 'mark', 'gravity', markName, direction))
  1680.     def mark_names(self):
  1681.         return self.tk.splitlist(self.tk.call(
  1682.             self._w, 'mark', 'names'))
  1683.     def mark_set(self, markName, index):
  1684.         self.tk.call(self._w, 'mark', 'set', markName, index)
  1685.     def mark_unset(self, *markNames):
  1686.         self.tk.call((self._w, 'mark', 'unset') + markNames)
  1687.     def scan_mark(self, x, y):
  1688.         self.tk.call(self._w, 'scan', 'mark', x, y)
  1689.     def scan_dragto(self, x, y):
  1690.         self.tk.call(self._w, 'scan', 'dragto', x, y)
  1691.     def search(self, pattern, index, stopindex=None,
  1692.            forwards=None, backwards=None, exact=None,
  1693.            regexp=None, nocase=None, count=None):
  1694.         args = [self._w, 'search']
  1695.         if forwards: args.append('-forwards')
  1696.         if backwards: args.append('-backwards')
  1697.         if exact: args.append('-exact')
  1698.         if regexp: args.append('-regexp')
  1699.         if nocase: args.append('-nocase')
  1700.         if count: args.append('-count'); args.append(count)
  1701.         if pattern[0] == '-': args.append('--')
  1702.         args.append(pattern)
  1703.         args.append(index)
  1704.         if stopindex: args.append(stopindex)
  1705.         return self.tk.call(tuple(args))
  1706.     def see(self, index):
  1707.         self.tk.call(self._w, 'see', index)
  1708.     def tag_add(self, tagName, index1, index2=None):
  1709.         self.tk.call(
  1710.             self._w, 'tag', 'add', tagName, index1, index2)
  1711.     def tag_unbind(self, tagName, sequence, funcid=None):
  1712.         self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
  1713.         if funcid:
  1714.             self.deletecommand(funcid)
  1715.     def tag_bind(self, tagName, sequence, func, add=None):
  1716.         return self._bind((self._w, 'tag', 'bind', tagName),
  1717.                   sequence, func, add)
  1718.     def tag_cget(self, tagName, option):
  1719.         if option[:1] != '-':
  1720.             option = '-' + option
  1721.         if option[-1:] == '_':
  1722.             option = option[:-1]
  1723.         return self.tk.call(self._w, 'tag', 'cget', tagName, option)
  1724.     def tag_configure(self, tagName, cnf={}, **kw):
  1725.         if type(cnf) == StringType:
  1726.             x = self.tk.split(self.tk.call(
  1727.                 self._w, 'tag', 'configure', tagName, '-'+cnf))
  1728.             return (x[0][1:],) + x[1:]
  1729.         self.tk.call(
  1730.               (self._w, 'tag', 'configure', tagName)
  1731.               + self._options(cnf, kw))
  1732.     tag_config = tag_configure
  1733.     def tag_delete(self, *tagNames):
  1734.         self.tk.call((self._w, 'tag', 'delete') + tagNames)
  1735.     def tag_lower(self, tagName, belowThis=None):
  1736.         self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
  1737.     def tag_names(self, index=None):
  1738.         return self.tk.splitlist(
  1739.             self.tk.call(self._w, 'tag', 'names', index))
  1740.     def tag_nextrange(self, tagName, index1, index2=None):
  1741.         return self.tk.splitlist(self.tk.call(
  1742.             self._w, 'tag', 'nextrange', tagName, index1, index2))
  1743.     def tag_prevrange(self, tagName, index1, index2=None):
  1744.         return self.tk.splitlist(self.tk.call(
  1745.             self._w, 'tag', 'prevrange', tagName, index1, index2))
  1746.     def tag_raise(self, tagName, aboveThis=None):
  1747.         self.tk.call(
  1748.             self._w, 'tag', 'raise', tagName, aboveThis)
  1749.     def tag_ranges(self, tagName):
  1750.         return self.tk.splitlist(self.tk.call(
  1751.             self._w, 'tag', 'ranges', tagName))
  1752.     def tag_remove(self, tagName, index1, index2=None):
  1753.         self.tk.call(
  1754.             self._w, 'tag', 'remove', tagName, index1, index2)
  1755.     def window_cget(self, index, option):
  1756.         if option[:1] != '-':
  1757.             option = '-' + option
  1758.         if option[-1:] == '_':
  1759.             option = option[:-1]
  1760.         return self.tk.call(self._w, 'window', 'cget', index, option)
  1761.     def window_configure(self, index, cnf={}, **kw):
  1762.         if type(cnf) == StringType:
  1763.             x = self.tk.split(self.tk.call(
  1764.                 self._w, 'window', 'configure',
  1765.                 index, '-'+cnf))
  1766.             return (x[0][1:],) + x[1:]
  1767.         self.tk.call(
  1768.               (self._w, 'window', 'configure', index)
  1769.               + self._options(cnf, kw))
  1770.     window_config = window_configure
  1771.     def window_create(self, index, cnf={}, **kw):
  1772.         self.tk.call(
  1773.               (self._w, 'window', 'create', index)
  1774.               + self._options(cnf, kw))
  1775.     def window_names(self):
  1776.         return self.tk.splitlist(
  1777.             self.tk.call(self._w, 'window', 'names'))
  1778.     def xview(self, *what):
  1779.         if not what:
  1780.             return self._getdoubles(self.tk.call(self._w, 'xview'))
  1781.         self.tk.call((self._w, 'xview') + what)
  1782.     def yview(self, *what):
  1783.         if not what:
  1784.             return self._getdoubles(self.tk.call(self._w, 'yview'))
  1785.         self.tk.call((self._w, 'yview') + what)
  1786.     def yview_pickplace(self, *what):
  1787.         self.tk.call((self._w, 'yview', '-pickplace') + what)
  1788.  
  1789. class _setit:
  1790.     def __init__(self, var, value):
  1791.         self.__value = value
  1792.         self.__var = var
  1793.     def __call__(self, *args):
  1794.         self.__var.set(self.__value)
  1795.  
  1796. class OptionMenu(Menubutton):
  1797.     def __init__(self, master, variable, value, *values):
  1798.         kw = {"borderwidth": 2, "textvariable": variable,
  1799.               "indicatoron": 1, "relief": RAISED, "anchor": "c",
  1800.               "highlightthickness": 2}
  1801.         Widget.__init__(self, master, "menubutton", kw)
  1802.         self.widgetName = 'tk_optionMenu'
  1803.         menu = self.__menu = Menu(self, name="menu", tearoff=0)
  1804.         self.menuname = menu._w
  1805.         menu.add_command(label=value, command=_setit(variable, value))
  1806.         for v in values:
  1807.             menu.add_command(label=v, command=_setit(variable, v))
  1808.         self["menu"] = menu
  1809.  
  1810.     def __getitem__(self, name):
  1811.         if name == 'menu':
  1812.             return self.__menu
  1813.         return Widget.__getitem__(self, name)
  1814.  
  1815.     def destroy(self):
  1816.         Menubutton.destroy(self)
  1817.         self.__menu = None
  1818.  
  1819. class Image:
  1820.     def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
  1821.         self.name = None
  1822.         if not master:
  1823.             master = _default_root
  1824.             if not master:
  1825.                 raise RuntimeError, 'Too early to create image'
  1826.         self.tk = master.tk
  1827.         if not name:
  1828.             name = `id(self)`
  1829.             # The following is needed for systems where id(x)
  1830.             # can return a negative number, such as Linux/m68k:
  1831.             if name[0] == '-': name = '_' + name[1:]
  1832.         if kw and cnf: cnf = _cnfmerge((cnf, kw))
  1833.         elif kw: cnf = kw
  1834.         options = ()
  1835.         for k, v in cnf.items():
  1836.             if callable(v):
  1837.                 v = self._register(v)
  1838.             options = options + ('-'+k, v)
  1839.         self.tk.call(('image', 'create', imgtype, name,) + options)
  1840.         self.name = name
  1841.     def __str__(self): return self.name
  1842.     def __del__(self):
  1843.         if self.name:
  1844.             try:
  1845.                 self.tk.call('image', 'delete', self.name)
  1846.             except TclError:
  1847.                 # May happen if the root was destroyed
  1848.                 pass
  1849.     def __setitem__(self, key, value):
  1850.         self.tk.call(self.name, 'configure', '-'+key, value)
  1851.     def __getitem__(self, key):
  1852.         return self.tk.call(self.name, 'configure', '-'+key)
  1853.     def configure(self, **kw):
  1854.         res = ()
  1855.         for k, v in _cnfmerge(kw).items():
  1856.             if v is not None:
  1857.                 if k[-1] == '_': k = k[:-1]
  1858.                 if callable(v):
  1859.                     v = self._register(v)
  1860.                 res = res + ('-'+k, v)
  1861.         self.tk.call((self.name, 'config') + res)
  1862.     config = configure
  1863.     def height(self):
  1864.         return getint(
  1865.             self.tk.call('image', 'height', self.name))
  1866.     def type(self):
  1867.         return self.tk.call('image', 'type', self.name)
  1868.     def width(self):
  1869.         return getint(
  1870.             self.tk.call('image', 'width', self.name))
  1871.  
  1872. class PhotoImage(Image):
  1873.     def __init__(self, name=None, cnf={}, master=None, **kw):
  1874.         apply(Image.__init__, (self, 'photo', name, cnf, master), kw)
  1875.     def blank(self):
  1876.         self.tk.call(self.name, 'blank')
  1877.     def cget(self, option):
  1878.         return self.tk.call(self.name, 'cget', '-' + option)
  1879.     # XXX config
  1880.     def __getitem__(self, key):
  1881.         return self.tk.call(self.name, 'cget', '-' + key)
  1882.     # XXX copy -from, -to, ...?
  1883.     def copy(self):
  1884.         destImage = PhotoImage()
  1885.         self.tk.call(destImage, 'copy', self.name)
  1886.         return destImage
  1887.     def zoom(self,x,y=''):
  1888.         destImage = PhotoImage()
  1889.         if y=='': y=x
  1890.         self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
  1891.         return destImage
  1892.     def subsample(self,x,y=''):
  1893.         destImage = PhotoImage()
  1894.         if y=='': y=x
  1895.         self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
  1896.         return destImage
  1897.     def get(self, x, y):
  1898.         return self.tk.call(self.name, 'get', x, y)
  1899.     def put(self, data, to=None):
  1900.         args = (self.name, 'put', data)
  1901.         if to:
  1902.             if to[0] == '-to':
  1903.                 to = to[1:]
  1904.             args = args + ('-to',) + tuple(to)
  1905.         self.tk.call(args)
  1906.     # XXX read
  1907.     def write(self, filename, format=None, from_coords=None):
  1908.         args = (self.name, 'write', filename)
  1909.         if format:
  1910.             args = args + ('-format', format)
  1911.         if from_coords:
  1912.             args = args + ('-from',) + tuple(from_coords)
  1913.         self.tk.call(args)
  1914.  
  1915. class BitmapImage(Image):
  1916.     def __init__(self, name=None, cnf={}, master=None, **kw):
  1917.         apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)
  1918.  
  1919. def image_names(): return _default_root.tk.call('image', 'names')
  1920. def image_types(): return _default_root.tk.call('image', 'types')
  1921.  
  1922. ######################################################################
  1923. # Extensions:
  1924.  
  1925. class Studbutton(Button):
  1926.     def __init__(self, master=None, cnf={}, **kw):
  1927.         Widget.__init__(self, master, 'studbutton', cnf, kw)
  1928.         self.bind('<Any-Enter>',       self.tkButtonEnter)
  1929.         self.bind('<Any-Leave>',       self.tkButtonLeave)
  1930.         self.bind('<1>',               self.tkButtonDown)
  1931.         self.bind('<ButtonRelease-1>', self.tkButtonUp)
  1932.  
  1933. class Tributton(Button):
  1934.     def __init__(self, master=None, cnf={}, **kw):
  1935.         Widget.__init__(self, master, 'tributton', cnf, kw)
  1936.         self.bind('<Any-Enter>',       self.tkButtonEnter)
  1937.         self.bind('<Any-Leave>',       self.tkButtonLeave)
  1938.         self.bind('<1>',               self.tkButtonDown)
  1939.         self.bind('<ButtonRelease-1>', self.tkButtonUp)
  1940.         self['fg']               = self['bg']
  1941.         self['activebackground'] = self['bg']
  1942.  
  1943. ######################################################################
  1944. # Test:
  1945.  
  1946. def _test():
  1947.     root = Tk()
  1948.     label = Label(root, text="Proof-of-existence test for Tk")
  1949.     label.pack()
  1950.     test = Button(root, text="Click me!",
  1951.               command=lambda root=root: root.test.configure(
  1952.                   text="[%s]" % root.test['text']))
  1953.     test.pack()
  1954.     root.test = test
  1955.     quit = Button(root, text="QUIT", command=root.destroy)
  1956.     quit.pack()
  1957.     # The following three commands are needed so the window pops
  1958.     # up on top on Windows...
  1959.     root.iconify()
  1960.     root.update()
  1961.     root.deiconify()
  1962.     root.mainloop()
  1963.  
  1964. if __name__ == '__main__':
  1965.     _test()
  1966.